package com.xmut.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmut.mall.coupon.dao.CouponDao;
import com.xmut.mall.coupon.entity.CouponEntity;
import com.xmut.mall.coupon.service.CouponService;
import com.xmut.mall.coupon.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.coupon.dao.PromotionGoodsDao;
import com.xmut.mall.coupon.entity.PromotionGoodsEntity;
import com.xmut.mall.coupon.service.PromotionGoodsService;

import java.util.List;
import java.util.stream.Collectors;


@Service("promotionGoodsService")
public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsDao, PromotionGoodsEntity> implements PromotionGoodsService {

    @Autowired
    private CouponDao couponDao;

    /**
     * 根据skuid获取该sku对应的优惠券
     * @param skuId
     * @return
     */
    @Override
    public RespBean getProductDetailCoupon(Long skuId) {
        List<PromotionGoodsEntity> promotionGoodsEntityList = this.list(new QueryWrapper<PromotionGoodsEntity>()
                .eq("sku_id", skuId));

        /**
         * 过滤出来属于优惠券的商品(因为该表包含消费券 满减 秒杀等使用的商品)
         */
        List<PromotionGoodsEntity> promotionGoodsEntitiesTypeEqCoupon = promotionGoodsEntityList.stream().filter(item -> {
            return item.getPromotionType().equals("COUPON");
        }).collect(Collectors.toList());

        /**
         * 过滤出来优惠券的ID
         */
        List<Long> CouponIds = promotionGoodsEntitiesTypeEqCoupon.stream()
                .map(PromotionGoodsEntity::getPromotionId).collect(Collectors.toList());

        List<CouponEntity> CouponList = couponDao.selectBatchIds(CouponIds);

        return RespBean.success("获取成功",CouponList);

    }
}